package com.ldzy.controller;

import com.ldzy.pojo.Result;
import com.ldzy.pojo.ResultCode;
import com.ldzy.pojo.User;
import com.ldzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Controller
public class UserController {
     @Autowired
     UserService userService;
    @RequestMapping("/toMain")//点击首页跳转到首页
    public String toMain(){
        return "/main.jsp";
    }
    @RequestMapping("/login")//点击登录进行验证
    public String login(User user, HttpServletRequest request) {
            User user1 = userService.login(user);
            if (user1 != null) {
                request.getSession().setAttribute("user_session", user1);
                int uClass=user1.getuClass();
                if (uClass==0){//如果是管理员，跳转到管理员页面
                    return "redirect:/admin/main.jsp";
                }else if (uClass==1){//员工跳转到员工页面
                    return "redirect:/admin/staff/indexB.jsp";
                }
            }
            request.setAttribute("msg", "用户名或密码错误");
            return "forward:/admin/login.jsp";
    }
    @RequestMapping("/loginOut")//点击退出登录
    public void loginOut(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        session.invalidate();
        request.setAttribute("msg", "退出成功");
        request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
    }
    @RequestMapping("/staff")//查询员工信息
    public ModelAndView staff(){
        List<User> user= userService.selectStaff();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("staff");
        modelAndView.addObject("view",user);
        return modelAndView;
    }
    @RequestMapping("/findById")//根据uid查询信息返回到修改信息页面
    @ResponseBody
    public Result<User> findById(int uid){
        User user=userService.findById(uid);
        if (user == null) {
            return new Result<>(false, ResultCode.ERROR.getDesc(), ResultCode.ERROR.getCode());
        } else {
            return new Result<>(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode(), user);
        }
    }
    @RequestMapping("/updateUser")//修改员工信息
    @ResponseBody
    public Result<User> updateUser(User user){
        int i=userService.updateUser(user);
        if (i>0){
            return new Result<>(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode());
        }else {
            return new Result<>(false, ResultCode.ERROR.getDesc(), ResultCode.ERROR.getCode());
        }
    }
    @RequestMapping("/addUser")//新增一个员工
    @ResponseBody
    public Result addUser(User user){
        int i=userService.addUser(user);
        if (i>0)return new Result<>(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode());
        return new Result<>(false, ResultCode.ERROR.getDesc(), ResultCode.ERROR.getCode());
    }
    @RequestMapping("/deleteUserById")//删除一个员工
    @ResponseBody
    public Result<User> deleteById(int uid) {
        int i = 0;
        try{
            i=userService.deleteById(uid);
        }catch (Exception e){
            return new Result<>(false, ResultCode.ERROR_DELECT_USER.getDesc(), ResultCode.ERROR_DELECT_USER.getCode());
        }
        if (i>0){
            return new Result<>(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode());
        }else {

            return new Result<>(false, ResultCode.ERROR.getDesc(), ResultCode.ERROR.getCode());
        }
    }
    @RequestMapping("/test")
    @ResponseBody
    public Result test(User user){
        String name=user.getName();
        String sex=user.getSex();
        System.out.println(sex);
        if (name!=null)return new Result<>(true, ResultCode.SUCCESS.getDesc(), ResultCode.SUCCESS.getCode());
        return new Result<>(false, ResultCode.ERROR.getDesc(), ResultCode.ERROR.getCode());
    }
}
